package com.hanggold.poi;

import org.apache.poi.sl.draw.DrawFactory;
import org.apache.poi.xslf.usermodel.*;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.util.List;

/**
 * @author <a href="mailto:sunmch@163.com">smc</a>
 * @date 2019-09-18 14:35
 * @since 1.0.0
 **/
public class PPT2PDF {


    private static String PPT_FILE = "H:" + File.separator + "yjc.pptx";

    private static String IMAGE_DIR = "H:" + File.separator + "ppt2img" + File.separator;

    private static String PPT_FILE_PROP_NAME = "ppt.file";

    private static String PPT_TO_IMG_DIR = "ppt.image.dir";


    public static void main(String[] args) throws Exception {

        PPT_FILE = System.getProperty(PPT_FILE_PROP_NAME, PPT_FILE);

        IMAGE_DIR = System.getProperty(PPT_TO_IMG_DIR, IMAGE_DIR);

        System.out.printf("ppt文件名称: %s \n", PPT_FILE);

        System.out.printf("ppt生成的图片目录: %s \n", IMAGE_DIR);

        long start = System.currentTimeMillis();

        File file = new File(PPT_FILE);

        XMLSlideShow ppt = new XMLSlideShow(new FileInputStream(file));

        Dimension pgsize = ppt.getPageSize();

        List<XSLFSlide> slides = ppt.getSlides();

        for (int i = 0; i < slides.size(); i++) {
            XSLFSlide xslfSlide = slides.get(i);

          List<XSLFShape> shapes = xslfSlide.getShapes();
            for (XSLFShape shape : shapes) {
                if (shape instanceof XSLFTextShape) {
                    XSLFTextShape sh = (XSLFTextShape) shape;
                    List<XSLFTextParagraph> textParagraphs = sh.getTextParagraphs();
                    for (XSLFTextParagraph xslfTextParagraph : textParagraphs) {
                        List<XSLFTextRun> textRuns = xslfTextParagraph.getTextRuns();
                        for (XSLFTextRun xslfTextRun : textRuns) {
                            xslfTextRun.setFontFamily("宋体");
                        }
                    }
                }
            }

            BufferedImage img = new BufferedImage(pgsize.width, pgsize.height, BufferedImage.TYPE_INT_RGB);
            Graphics2D graphics = img.createGraphics();
            graphics.setPaint(Color.white);
            graphics.fill(new Rectangle2D.Float(0, 0, pgsize.width, pgsize.height));
            xslfSlide.draw(graphics);
            DrawFactory.getInstance(graphics).fixFonts(graphics);

            // default rendering options
            graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            graphics.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
            graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
            graphics.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
            graphics.scale(1, 1);

            ImageIO.write(img, "png", new File(IMAGE_DIR + (i) + ".png"));
            img.flush();
        }

        System.out.println("Image successfully created");

        long end = System.currentTimeMillis();

        System.out.printf("转换pdf花费时间 :  %d", (end - start));

    }

}
